package _interview100;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 438. 找到字符串中所有字母异位词
 */
public class No438 {
    public List<Integer> findAnagrams(String s, String p) {
        int size = p.length();
        char[] curArr = new char[size];
        for (int i = 0; i < size; i++) {
            curArr[i] = p.charAt(i);
        }
        List<Integer> answer = new ArrayList<>();
        for (int j = 0; j < s.length() - size + 1; j++) {
            char[] temp = s.substring(j, j + size).toCharArray();
            Arrays.sort(temp);
            Arrays.sort(curArr);
            if (new String(temp).equals(new String(curArr))) {
                answer.add(j);
                while (j < s.length() - size && s.charAt(j) == s.charAt(j + size)) {
                    j++;
                    answer.add(j);
                }
            }else{
                while (j < s.length() - size && s.charAt(j) == s.charAt(j + size)) {
                    j++;
                }
            }
        }
        return answer;
    }
}
